package com.ztgf.logistics.service.deppon;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.ztgf.common.constant.Result;
import com.ztgf.common.enums.CodeEnum;
import com.ztgf.shopping.models.dto.shop.ShopContractSettleInDTO;
import com.ztgf.shopping.models.service.shop.IShopContractSettleInService;
import org.apache.dubbo.config.annotation.DubboReference;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.BeanUtils;
import javax.annotation.Resource;
import com.ztgf.logistics.common.config.YmlConfig;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.ztgf.logistics.mapper.deppon.LogisticsUserContractMapper;
import com.ztgf.logistics.models.service.deppon.ILogisticsUserContractService;
import com.ztgf.logistics.models.bo.deppon.LogisticsUserContractSaveBO;
import com.ztgf.logistics.models.dto.deppon.LogisticsUserContractDTO;

/**
 * 物流合同关系表控制器
 *
 * @author mxli
 * @Date 2021-09-13 22:17:03
 */
@DubboService
public class LogisticsUserContractServiceImpl extends ServiceImpl<LogisticsUserContractMapper, LogisticsUserContractDTO> implements ILogisticsUserContractService{

    @Resource
    private YmlConfig ymlConfig;
    @DubboReference
    private IShopContractSettleInService shopContractSettleInService;
    @Override
    public Result add(LogisticsUserContractDTO dto) {
        Long id = ymlConfig.getPkId();
        dto.setId(id);
        this.insert(dto);
        String url=null;
        boolean flag = shopContractSettleInService.createDeBangContract(dto.getShopId(),dto.getId());
        if (flag){
            //生成合同成功
            try {
                //返回合同地址
                 url = shopContractSettleInService.gerenateDBSignUrl(dto.getShopId(),dto.getId()); //获取合同签订地址
                if (url==null || url==""){
                    return new Result(CodeEnum.FAIL_COMMODITY_AUDIT_330200);
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }else {
            return new Result(CodeEnum.FAIL_COMMODITY_AUDIT_330100);
        }
        return new Result(url);
    }

    @Override
    public LogisticsUserContractDTO getByName(String name){
        Wrapper<LogisticsUserContractDTO> wrapper = new EntityWrapper<>();
        wrapper.eq("contract_id", name);
        return this.selectOne(wrapper);
    }
}
